using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
//using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuThuData.
	/// </summary>
	public class frmTuVanEnquiryData: TMSDataBase  {

		public enum TL  {
			Enquiry,
			Followup,
            HinhThuc,
            GioiTinh,
            QuanThuongTru,
            ThanhPhoThuongTru,
            QuanTamTru,
            ThanhPhoTamTru,
            NgheNghiep,
            KenhThongTin,
			ChuongTrinhQuanTam,
			BuoiHoc,
			GioHoc,
            KhaNangTaiChinh,
            PHUHUYNH,
            HINHANH,
            DM_QUANHE,
            MaCoSo
		}

		private string sqlEnquiry = 
			"SELECT	[EnquiryNumber], [EnquiryDate], [MaDonVi], [HinhThuc], [Holot], [Ten], " +
			"		[GioiTinh], [NgaySinh], [DiaChiThuongTru], [QuanThuongTru], [ThanhPhoThuongTru], [DiaChiTamTru], [QuanTamTru], [ThanhPhoTamTru], " +
			"		[DienThoaiCoDinh], [DienThoaiDiDong], [Email], [SoCMND], [NgheNghiep], [SVNamMay], " +
            "		[HoTenPhuHuynh], [DienThoaiPhuHuynh], " +
            "		[KenhThongTin], [ChuongTrinhQuanTam], [BuoiHoc], [GioHoc], " +
            "		[KhaNangTaiChinh], [NguoiTaiTro], [Counsellor], [UserEdit], [IsDuHoc], [Facebook], [GooglePlus], [MaCoSo]" +
            "FROM 	[ENQUIRY] ";

		private string sqlFollowup = 
			"SELECT [ID], EnquiryNumber, Ngay, TuVan, NoiDung " +
			"FROM   FOLLOWUP ";

        private string sqlPhuHuynh =
            @"SELECT  MaPhuHuynh, EnquiryNumber, TenPhuHuynh, Phone, MaQuanHe 
                FROM PHUHUYNH ";

        private string sqlHinhAnh =
            @"SELECT [ID], EnquiryNumber, DienGiai, NoiDung 
                FROM HINHANH ";

		public frmTuVanEnquiryData() {

			ds.Tables.Add("Enquiry");
			ds.Tables.Add("Followup");
			ds.Tables.Add("GioiTinh");
            ds.Tables.Add("HinhThuc");
            ds.Tables.Add("QuanThuongTru");
            ds.Tables.Add("ThanhPhoThuongTru");
            ds.Tables.Add("QuanTamTru");
            ds.Tables.Add("ThanhPhoTamTru");
            ds.Tables.Add("NgheNghiep");
            ds.Tables.Add("KenhThongTin");
			ds.Tables.Add("ChuongTrinhQuanTam");
			ds.Tables.Add("BuoiHoc");
            ds.Tables.Add("GioHoc");
            ds.Tables.Add("KhaNangTaiChinh");
            ds.Tables.Add("PhuHuynh");
            ds.Tables.Add("HinhAnh");
            ds.Tables.Add("QuanHe");
            ds.Tables.Add("CoSo");

            DanhMuc.fill(getTable(TL.HinhThuc), DanhMuc.TLDM.HinhThucEnq); 
            DanhMuc.fill(getTable(TL.GioiTinh), DanhMuc.TLDM.GioiTinh);
            DanhMuc.fill(getTable(TL.QuanThuongTru), DanhMuc.TLDM.Quan);
            DanhMuc.fill(getTable(TL.ThanhPhoThuongTru), DanhMuc.TLDM.ThanhPho);
            DanhMuc.fill(getTable(TL.QuanTamTru), DanhMuc.TLDM.Quan);
            DanhMuc.fill(getTable(TL.ThanhPhoTamTru), DanhMuc.TLDM.ThanhPho);
            DanhMuc.fill(getTable(TL.NgheNghiep), DanhMuc.TLDM.NgheNghiep);
            DanhMuc.fill(getTable(TL.KenhThongTin), DanhMuc.TLDM.KenhThongTin);
			DanhMuc.fill(getTable(TL.ChuongTrinhQuanTam), DanhMuc.TLDM.ChuongTrinhQuanTam);
			DanhMuc.fill(getTable(TL.BuoiHoc), DanhMuc.TLDM.BuoiHoc);
            DanhMuc.fill(getTable(TL.GioHoc), DanhMuc.TLDM.GioHoc);
            DanhMuc.fill(getTable(TL.KhaNangTaiChinh), DanhMuc.TLDM.KhaNangTaiChinh);
            DanhMuc.fill(getTable(TL.DM_QUANHE), DanhMuc.TLDM.QuanHe);
            DanhMuc.fill(getTable(TL.MaCoSo), DanhMuc.TLDM.CoSo);

			initData("('')", true);
		}

		public void initData(string strEnquiryNumber, bool firstLoading) {
			if (!firstLoading) {
                getTable(TL.DM_QUANHE).Clear();
                getTable(TL.PHUHUYNH).Clear();
                getTable(TL.HINHANH).Clear();
				getTable(TL.Followup).Clear();
				getTable(TL.Enquiry).Clear();
			}

            SQLHelper.Fill(getTable(TL.Enquiry), GlobalVariable.WSInfo.ConnectionString,
                sqlEnquiry  + "WHERE [EnquiryNumber] In " + strEnquiryNumber );

            SQLHelper.Fill(getTable(TL.Followup), GlobalVariable.WSInfo.ConnectionString,
                sqlFollowup  + "WHERE Followup.EnquiryNumber In " + strEnquiryNumber );

            SQLHelper.Fill(getTable(TL.PHUHUYNH), GlobalVariable.WSInfo.ConnectionString,
                sqlPhuHuynh + "WHERE PHUHUYNH.EnquiryNumber IN " + strEnquiryNumber);

            SQLHelper.Fill(getTable(TL.HINHANH), GlobalVariable.WSInfo.ConnectionString,
                sqlHinhAnh + "WHERE HINHANH.EnquiryNumber IN " + strEnquiryNumber);

			if (firstLoading) {
				// Set primary key(s)
				getTable(TL.Enquiry).PrimaryKey = new DataColumn[]{getTable(TL.Enquiry).Columns["EnquiryNumber"]};
				getTable(TL.Followup).PrimaryKey = new DataColumn[]{getTable(TL.Followup).Columns["ID"]};
                getTable(TL.DM_QUANHE).PrimaryKey = new DataColumn[]{getTable(TL.DM_QUANHE).Columns["MaQuanHe"]};
                getTable(TL.PHUHUYNH).PrimaryKey = new DataColumn[]{getTable(TL.PHUHUYNH).Columns["MaPhuHuynh"]};
                getTable(TL.HINHANH).PrimaryKey = new DataColumn[] { getTable(TL.HINHANH).Columns["ID"] };

				// Set relations
				ds.Relations.Add("M_D", getTable(TL.Enquiry).Columns["EnquiryNumber"],
					getTable(TL.Followup).Columns["EnquiryNumber"]);

                ds.Relations.Add("Enquiry_PhuHuynh", getTable(TL.Enquiry).Columns["EnquiryNumber"],
                                    getTable(TL.PHUHUYNH).Columns["EnquiryNumber"]);

                ds.Relations.Add("Enquiry_HinhAnh", getTable(TL.Enquiry).Columns["EnquiryNumber"],
                                    getTable(TL.HINHANH).Columns["EnquiryNumber"]);

                /*
                ds.Relations.Add("QuanHe_PhuHuynh", getTable(TL.DM_QUANHE).Columns["MaQuanHe"],
                                    getTable(TL.PHUHUYNH).Columns["MaQuanHe"]);
                 */

				// Set default values
				//getTable(TL.Enquiry).Columns["EnquiryNumber"].AutoIncrement  = true ;
                //getTable(TL.Enquiry).Columns["EnquiryNumber"].AutoIncrementStep = 1;
                getTable(TL.Enquiry).Columns["EnquiryNumber"].DefaultValue = "";
                getTable(TL.Enquiry).Columns["EnquiryDate"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.Enquiry).Columns["MaDonVi"].DefaultValue = GlobalVariable.WSInfo.CompanyID;
                getTable(TL.Enquiry).Columns["Counsellor"].DefaultValue = GlobalVariable.WSInfo.UserID;
                getTable(TL.Enquiry).Columns["UserEdit"].DefaultValue = GlobalVariable.WSInfo.UserID;
                getTable(TL.Enquiry).Columns["IsDuHoc"].DefaultValue = false;
                getTable(TL.Enquiry).Columns["Facebook"].DefaultValue = "";
                getTable(TL.Enquiry).Columns["GooglePlus"].DefaultValue = "";

				//Detail
                getTable(TL.Followup).Columns["ID"].AutoIncrement = true;
                getTable(TL.Followup).Columns["ID"].AutoIncrementStep = 1;
                getTable(TL.Followup).Columns["TuVan"].DefaultValue = GlobalVariable.WSInfo.UserID;
                getTable(TL.Followup).Columns["Ngay"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.Followup).Columns["NoiDung"].DefaultValue = "";

                //getTable(TL.DM_QUANHE).Columns["MaQuanHe"].DefaultValue = "";

                getTable(TL.PHUHUYNH).Columns["MaPhuHuynh"].AutoIncrement = true;
                getTable(TL.PHUHUYNH).Columns["MaPhuHuynh"].AutoIncrementStep = 1;

                getTable(TL.HINHANH).Columns["ID"].AutoIncrement = true;
                getTable(TL.HINHANH).Columns["ID"].AutoIncrementStep = 1;
			}
           
		}

		public bool createNewRow() {
			try {

                initData("('')", false);

				DataRow dr = getTable(TL.Enquiry).NewRow();
                vg.DateForGenerating = WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "EQ";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
				dr["EnquiryNumber"] = vg.generateVoucherNo("Enquiry" ,"[EnquiryNumber]" );
				getTable(TL.Enquiry).Rows.Add(dr);

                return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
                //longnt comment----------------
                //new SimpleMasterDetailUpdater(sqlEnquiry, sqlFollowup, getTable(TL.Enquiry), getTable(TL.Followup), GlobalVariable.WSInfo.ConnectionString);

                new SimpleMasterDetailUpdater1(sqlEnquiry, sqlFollowup, sqlPhuHuynh, sqlHinhAnh, getTable(TL.Enquiry), getTable(TL.Followup), getTable(TL.PHUHUYNH), getTable(TL.HINHANH), GlobalVariable.WSInfo.ConnectionString);

				ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}

	}
}
